﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using olierFrm.DataBase.FrameWork.Business;
using System.Web;

namespace olierFrm.WebFormDev.Session
{
    [Serializable]
    public class SModule
    {

        public SModule()
        {
            //if (HttpContext.Current.Session[SessionID] == null)
            //    return;
            _diModule = new Dictionary<string, string>();
            //var su = (SessionModule)HttpContext.Current.Session[SessionID];
        }

        public const string SessionID = "SessionModule";
        /// <summary>
        /// 模块信息，Key：模块ID、Value：模块名称
        /// </summary>
        private Dictionary<string, string> _diModule { get; set; }

        /// <summary>
        /// 增加模块
        /// </summary>
        /// <param name="ModuleID">模块编号</param>
        /// <param name="ModuleName">模块名称</param>
        public void AddModule(string ModuleID, string ModuleName)
        {
            if (_diModule.ContainsKey(ModuleID)) return;
            _diModule.Add(ModuleID, ModuleName);
            BusinessSQL bs = new BusinessSQL("SystemLogModify");
            bs.AddSystemParams();
            bs.SetParam("ModuleID", ModuleID);
            bs.SetParam("LogType", "2");
            bs.SetParam("UpdateFlag", "I");
            bs.SetParam("Memo", "进入[" + ModuleName + "]模块");
            bs.ExecuteSQL();
        }

        /// <summary>
        /// 移除模块
        /// </summary>
        /// <param name="ModuleID">模块编号</param>
        public void RemoveModule(string ModuleID)
        {
            if (!_diModule.ContainsKey(ModuleID)) return;
            _diModule.Remove(ModuleID);
            BusinessSQL bs = new BusinessSQL("SystemLogModify");
            bs.AddSystemParams();
            bs.SetParam("UpdateFlag", "U");
            bs.SetParam("ModuleID", ModuleID);
            bs.ExecuteSQL();
        }

        /// <summary>
        /// 移除所有模块
        /// </summary>
        public void RemoveAllModule()
        {
            BusinessSQL bs = new BusinessSQL("SystemLogModify");
            foreach (var k in _diModule.Keys)
            {
                bs.AddSystemParams();
                bs.SetParam("UpdateFlag", "U");
                bs.SetParam("ModuleID", k);
                bs.ExecuteSQL();
            }
            _diModule.Clear();
        }

        public static SModule Session
        {
            get
            {
                if (HttpContext.Current.Session == null || HttpContext.Current.Session[SessionID] == null)
                {
                    HttpContext.Current.Session.Add(SessionID, new SModule());
                }
                return (SModule)HttpContext.Current.Session[SessionID];
            }
            set
            {
                HttpContext.Current.Session[SessionID] = value;
            }
        }
    }
}
